'use strict';
const db = uniCloud.database();
const _ = db.command;

exports.main = async (event, context) => {
  const topN = event.top || 3;

  try {
    // Step 1: 从 consultants 表获取评分最高的前 N 位咨询师
    const res = await db.collection('consultants')
      .where({
        rating: _.gt(0) // 可选条件，排除未评分者
      })
      .orderBy('rating', 'desc')
      .limit(topN)
      .get();

    // Step 2: 提取所需字段
    const result = res.data.map(con => ({
      name: con.name || '未知',
      avatar: con.avatar || '',
      rating: con.rating || 0
    }));

    return {
      success: true,
      result
    };

  } catch (err) {
    console.error('获取评分前三咨询师失败:', err);
    return {
      success: false,
      message: '获取评分前三咨询师失败',
      error: err.message
    };
  }
};
